﻿@page "/Course"
@attribute [Authorize]
@layout ManagerLayout

@inject NavigationManager NavigationManager
@inject ICourseService CourseService
@inject IUserService UserService 
@inject IMessageService MessageService

<PageTitle>课程管理</PageTitle>

<AuthorizeView  Roles="管理员" Context="authorContext">
	<Authorized>
		<Layout>
			<Header>
                <div style="float:right">
					<AddCourseButton OnAddSuccess="async ()=>{_courses = await CourseService.GetAllAsync(0, 10);StateHasChanged();}" />
				</div>
			</Header>
			<Content>
				@if (_courses != null && _courses.Count > 0)
				{
					<Table TItem="Models.Course"
						   DataSource="@_courses"
						   Size="TableSize.Small">
						<PropertyColumn Title="名称" Property="c=>c.Name">
							<p>@context.Name</p>
						</PropertyColumn>
						<PropertyColumn Title="专业" Property="c=>c.DepartmentTypeId">
							<p>@context.DepartmentType.Name</p>
						</PropertyColumn>
						<ActionColumn Title="操作">
							<Button Icon="user" Type="primary" @onclick="_=>open(context.CourseId)">用户</Button>
							<UpdateCourseButton Course="context" OnAddSuccess="async ()=>_courses = await CourseService.GetAllAsync(0, 10)" />
							<Button Icon="delete" Type="primary" @onclick="()=>OpenDeleteConfirmModal(context)">删除</Button>
						</ActionColumn>
					</Table>
					
					<Drawer Closable="true" Width="720" Visible="visible" Title='("添加删除用户")' OnClose="_=>close()">
						<Template style="height:80%">
							<CourseUserForm CourseId="@_currentCourseId" UserWithCourses="@(_userWithCourses??new())"/>
						</Template>
					</Drawer>

					<Modal Title="确定删除"
						   Visible="@_modalVisible"
						   OnOk="@HandleModalOk"
						   OnCancel="()=>_modalVisible=false">
						@if (_selectCourse != null)
						{
							<Alert Type="@AlertType.Warning"
								   Message="@($"你真的要删除课程 {_selectCourse.Name} 吗?")"
								   Description="课程将会永久失去，相关信息将会被清空"
								   ShowIcon="true"
								   Closable />
						}						
					</Modal>
				}
				else
				{
					<Empty/>
				}
			</Content>
		</Layout>		
	</Authorized>
</AuthorizeView>

<AuthorizeView Roles="教师,学生">
    <Authorized>
		@if (_courses != null && _courses.Count > 0)
		{
			foreach (var course in _courses)
			{				
				<Card Hoverable="true" Title="@course.Name" >
					<Extra>
						<a	@onclick="()=>NavigateToDetail(course.CourseId.ToString())">查看</a>
					</Extra>
				</Card>
			}
		}
		else
		{
			<Empty></Empty>
		}
	</Authorized>
</AuthorizeView>

@code {
	[CascadingParameter]
	private Task<AuthenticationState>? _authenticationState { get; set; }

	private List<Models.Course> _courses { get; set; }
	private Guid userId;

	protected override async Task OnParametersSetAsync()
	{
		var state = await _authenticationState;
		AuthService.TryGetUserFromClaim(state.User, out userId, out string userName, out string userRole);
		if (userRole == "教师"||userRole=="学生")
		{
			if (userId != Guid.Empty && _courses == null)
			{
				var response = await CourseService.GetByUserIdAsync(userId.ToString());

				if (response.Success && response.Data != null)
				{
					_courses = response.Data;
					StateHasChanged();
				}
			}
		}
		if (userRole == "管理员")
		{
			if (_courses == null)
			{
				_courses = await CourseService.GetAllAsync(0, 10);
				StateHasChanged();
			}			
		}
		await base.OnParametersSetAsync();
	}

	private void NavigateToDetail(string courseId)
	{
		NavigationManager.NavigateTo($"course/{courseId}");
	}

	#region -课程用户管理-

	// 开关课程用户操作界面
	bool visible = false;
	private Guid _currentCourseId;
	void open(Guid courseId)
	{
		// 打开的同时记录课程编号
		_currentCourseId = courseId;
		this.visible = true;
		GetUserWithCourseId(courseId);
	}
	void close()
	{
		this.visible = false;
	}

	private List<UserWithCourse> _userWithCourses=new();
	// 获取课程所在专业的用户
	// 或许需要加入对其它专业的检索
	private async void GetUserWithCourseId(Guid courseId)
	{
		_userWithCourses = await UserService.GetUserWithCourseAsync(courseId);
	}
	#endregion

	private bool _modalVisible = false;
	private Models.Course? _selectCourse;
	private void OpenDeleteConfirmModal(Models.Course course)
	{
		_selectCourse = course;
		_modalVisible = true;
	}
	private async void HandleModalOk()
	{
		if (_selectCourse != null)
		{
			bool isSuccess=await CourseService.DeleteByIdAsync(_selectCourse.CourseId);
			if (isSuccess)
			{			
				_courses.Remove(_selectCourse);				
				MessageService.Success("已完成删除操作");
			}
		}
		
		_modalVisible = false;
		StateHasChanged();
	}
}
